<template>
    <div class="observer-lbrary-pages">
        <compSearchCriteria
         :formItem.sync="formItem" 
         @searchHandleChange="searchHandleChange" 
         v-model="dataTableConfig.requestParams"
         ref="refSearchCriteria"
         />
         <div class="btn-box" style="margin: 2px 0;">
            <div></div>
            <a-form layout="inline" style="display: flex;justify-content: flex-end;">
                <a-form-item>
                    <a-button class="blue-btn" type="primary" @click="increaseClick" icon>新增</a-button>
                </a-form-item>
                <a-form-item>
                    <a-button class="red-btn" type="primary" @click="batchDeletion" >批量删除</a-button>
                </a-form-item>
            </a-form>
         </div>
         
        <div>
            <compTable
             :key="compSelectorkey" 
             :config='dataTableConfig' 
             :columns="columns" 
             :requestParamObj="requestParams" 
             @selectedRowKeys="selectedRowKeys"
             @sorterChange="sorterChange"
             >
                <!-- <template v-slot:url="scope">
                    <span>{{ scope }}</span>
                </template> -->
                <template v-slot:operation="{record}" data-desc="自定操作栏">
                    <a-form layout="inline" class="search-form">
                        <!-- <a-form-item >
                            <a-button class="blue-btn" type="primary" @click="onlineEditingClick(record)">在线编辑</a-button>
                        </a-form-item> -->
                        <a-form-item >
                            <a-button class="blue-btn" type="primary" @click="reviseClick(record)">编辑</a-button>
                        </a-form-item>
                        <a-form-item>
                            <a-button class="blue-btn" type="primary" icon="download" :loading="DownloadTemplateLoading" @click="DownloadTemplateClick(record[0])">下载模板</a-button>
                        </a-form-item>
                        <a-form-item >
                            <a-button class="red-btn" type="primary" @click="deleteClick(record)" >删除</a-button>
                        </a-form-item>
                    </a-form>
                </template>
            </compTable>
        </div>
        <!-- <a-modal
         :visible="addEditVisible" 
         :title="addEditParameter.id?'编辑':'新增'" 
         @ok="handleOk" 
         :confirmLoading="confirmLoading" 
         @cancel="visible = false"> -->

         <increase ref="refModel"  @handleOk="handleOk"/>
            
        <!-- </a-modal> -->
        
    </div>
</template>
<script>
import compSearchCriteria from '@/components/compSearchCriteria/compSearchCriteria'
import compTable from '@/components/compTable/compTable'
import {typeInput, typeNumber, typeSelect, typeCheckbox, typeRadio} from '@/components/compSearchCriteria/ContractType'
import assessCheckApi from './service/api.js'
import increase from './increase.vue'
import { deleteArrServe, leadingOutFormwork, onlineEditingServe } from './service/index'
import blobType from '@/common/blobType'
import Vue from 'vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'
// import { CodeSandboxCircleFill } from '@yuncheng/ych-components'
  export default {
    components:{
        compSearchCriteria,
        compTable,
        increase
    },
    
    data(){
        return{
            addEditParameter:{},
            addEditVisible:false,
            selectedRowKeysArr:[],//选中表格数据
            compSelectorkey:'',
            requestParams:{},
            formItem:[
                {
                    label: '模板标题',
                    model: 'fmbbt',
                }

            ],
            columns: [
            {
                    title: '模板标题', //列头显示文字
                    dataIndex: 'fmbbt',
                    key: 'fmbbt',
                    width:300,
                },{
                    title: '模板类型1', //列头显示文字
                    dataIndex: 'fmblx1',
                    key: 'fmblx1',
                },{
                    title: '业务类型', //列头显示文字
                    dataIndex: 'fmblx2',
                    key: 'fmblx2',
                },{
                    title: '模板类型3', //列头显示文字
                    dataIndex: 'fmblx3',
                    key: 'fmblx3',
                },{
                    title: '模板类型4', //列头显示文字
                    dataIndex: 'fmblx4',
                    key: 'fmblx4',
                },{
                    title: '操作',
                    dataIndex: 'id',
                    scopedSlots: {customRender: 'operation'}, // 自定插槽格式
                    fixed: 'right',
                    width:300,
                }
            ],
            dataTableConfig:{
                api: assessCheckApi.list,
                rowKey: 'id',
                rowName: 'fgcyxm',
                rowSelection: {
                    // selectedRowKeys: null,
                    // type: EselectionType.checkbox
                },
                fywflcode:blobType.fywflcodeArr[blobType.menuType.indexOf(this.$route.name.substring(this.$route.name.indexOf('__'),this.$route.name.indexOf('_a')+2))],
            },
            leadingOutLoading:false,
            DownloadTemplateLoading:false,
        }
    },
    
    methods:{
        sorterChange(val,sorter){//获取到排序的数据
            //console.log('获取到排序的数据',val,sorter)
        },
        renovateSearchTabel(){//刷新搜索、表格
            this.compSelectorkey = Date.now()
            this.$refs.refSearchCriteria.resetEvents()
        },
        searchHandleChange(val){//获取搜索数据
            if(val.resetEvents){
                this.compSelectorkey = Date.now()
            }
            this.dataTableConfig.requestParams={...val}
            this.requestParams = {...this.requestParams,...val}
        },
        selectedRowKeys(keys){//获取选中表格rowKey
            this.selectedRowKeysArr = keys
        },
        increaseClick(){//新增
            this.addEditParameter = {
                fmbbt:'',
                fmblx1:'',
                fmblx2:'',
                fmblx3:'',
                fmblx4:'',
            }
            this.$refs.refModel.visibleShow = true
            this.$refs.refModel.getData(this.addEditParameter)
        },
        DownloadTemplateClick(val){//下载模板
            var that = this
            this.DownloadTemplateLoading = true
            var fileName = val.fmbbt
            let param = { fywflcode:blobType.fywflcodeArr[blobType.menuType.indexOf(this.$route.name.substring(this.$route.name.indexOf('__'),this.$route.name.indexOf('_a')+2))],id:val.id}
            //console.log(val)
            //console.log(!val.fwjlx)
            
            if(!val.fwjlx){
                this.DownloadTemplateLoading = false
                this.$message.warning('暂无模板文件')
                return
            }
            var fileType = ['.doc','.docx','.xls','.xlsx']
            leadingOutFormwork(param).then((data) => {
                this.DownloadTemplateLoading = false
                if (!data) {
                    that.$message.warning('文件下载失败')
                    return
                }
                let currentType = ''
                if(fileType.indexOf(val.fwjlx) == 0){
                    currentType = 'application/msword'
                }else if(fileType.indexOf(val.fwjlx) == 1){
                    currentType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
                }else if(fileType.indexOf(val.fwjlx) == 2){
                    currentType = 'application/vnd.ms-excel'
                }else if(fileType.indexOf(val.fwjlx) == 3){
                    currentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
                }
                    let url = window.URL.createObjectURL(new Blob([data], { type: currentType }))
                    let link = document.createElement('a')
                    link.style.display = 'none'
                    link.href = url
                    link.setAttribute('download', fileName + val.fwjlx)
                    //console.log('url----------',url)
                    document.body.appendChild(link)
                    link.click()
                    document.body.removeChild(link) //下载完成移除元素
                    window.URL.revokeObjectURL(url) //释放掉blob对象
            })
        },
        batchDeletion(){//
            var that = this
            this.$confirm({
                title:'您确认要删除吗？',
                // icon: createVNode(ExclamationCircleOutlined),
                onOk() {
                    if(that.selectedRowKeysArr.length){
                        let deleteString = that.selectedRowKeysArr.join(',')
                        deleteArrServe({ids:deleteString}).then(res=>{
                            if(res.code == 200){
                                that.$message.success('删除成功')
                            }else{
                                that.$message.warning(res.message)
                            }              
                            that.renovateSearchTabel()
                        })
                    }else{
                        that.$message.warning('请选择删除项')
                    }
                },
                
            });
            
            
        },
        handleOk(val,id){
            if(val && !id){
                this.renovateSearchTabel()
            }else if(val && id){
                this.requestParams = {
                    ...this.requestParams,
                    renovate:true,
                }
            }
            
        },
        
        //编辑
        reviseClick(val){
            var data = val[0]
            this.addEditParameter = {
                id:data.id,
                fmbbt:data.fmbbt,
                fmblx1:data.fmblx1,
                fmblx2:data.fmblx2,
                fmblx3:data.fmblx3,
                fmblx4:data.fmblx4,
                fwjccmc:data.fwjccmc,
                fcflj:data.fcflj,
                fwjlx:data.fwjlx,
                fmbzt:data.fmbzt,
                fmsmbnr:data.fmsmbnr,
                fmsmbbt:data.fmsmbbt,
                fjcxmbt:data.fjcxmbt,
            }
            this.$refs.refModel.visibleShow = true
            this.$refs.refModel.getData(this.addEditParameter)
        },
        //删除
        deleteClick(val){
            var that = this
            this.$confirm({
                title:'您确认要删除吗？',
                // icon: createVNode(ExclamationCircleOutlined),
                onOk() {
                    deleteArrServe({ids:val[0].id}).then(res=>{
                        if(res.code == 200){
                            that.$message.success('删除成功')
                        }else{
                            that.$message.warning(res.message)
                        }              
                        that.renovateSearchTabel()
                    })
                },
                
            });
            
            
        },
        AddHandleOk(){
            this.renovateSearchTabel()
        }
    },

  }
</script>
<style scoped>
.observer-lbrary-pages{
    background: #fff;
}
.btn-box{
    display: flex;
    justify-content: space-between;
}
.blue-btn{
    border: 1px solid #5ca3e5 !important;
    background-color: #5ca3e5 !important;
    color: #fff !important;
}
.red-btn{
    border: 1px solid #ed5565 !important;
    background-color: #ed5565 !important;
}

</style>